package com.ecmcloud.flow.node.impl;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import com.ecmcloud.common.constants.ProcessInstanceConstant;
import com.ecmcloud.common.dto.flow.Node;
import com.ecmcloud.common.dto.flow.NodeUser;
import com.ecmcloud.common.utils.JsonMapperUtils;
import com.ecmcloud.flow.node.AssignUserStrategy;
import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.collect.Lists;

/**
 * 来自表单
 */
@Component
public class AssignUserFormStrategyImpl implements InitializingBean, AssignUserStrategy {
	
    @Override
    public List<String> handle(Node node, NodeUser rootUser, Map<String, Object> variables) {
        List<String> assignList = Lists.newArrayList();
        //表单值
        Object variable = variables.get(node.getFormUserId());
        if (Objects.nonNull(variable)) {
            String jsonString = JsonMapperUtils.toJsonString(variable);
            List<NodeUser> nodeUserDtoList = JsonMapperUtils.getInstance().parseObject(jsonString, new TypeReference<List<NodeUser>>() {});
            List<String> userIdList = nodeUserDtoList.stream().map(w -> String.valueOf(w.getId())).collect(Collectors.toList());
            assignList.addAll(userIdList);
        }
        return assignList;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        afterPropertiesSet(ProcessInstanceConstant.AssignedTypeClass.FORM_USER);
    }
}
